# -*- coding: utf-8 -*

import urllib2
import socket

class readUrl:
    def __init__(self, retry):
        self.retry = retry;
        socket.setdefaulttimeout(10.0)
        
    def openUrl(self, url):
        for i in range(0, self.retry):
           html = self.tryUrl(url)
           if(html!=None):
               return html;
           elif(i < self.retry -1):
               print "Retrying to open " + url + ". Number: "  + str(i+1)
        print "Give up..."

    def tryUrl(self, url):
        try:
            c=urllib2.urlopen(url)
            return c.read()
        except:
            print "Could not open " + url
            return None

if __name__ == '__main__':
    crawler = readUrl(5)
    html = crawler.openUrl("http://www.chinadaily.com.cn/cndy/2008-01/02/index1.html")
    #html = crawler.openUrl("http://www.qq.com")
    outputfile = open('index1.html','w');
    outputfile.write(html);
    print html;
